package com.zjb.wxlisener.service.impl;

import com.google.gson.JsonObject;
import com.zjb.wxlisener.JavaBean.PCuser;
import com.zjb.wxlisener.JavaBean.User;
import com.zjb.wxlisener.mapper.LoginMapper;
import com.zjb.wxlisener.service.LoginService;
import com.zjb.wxlisener.until.QiNiuUtil;
import com.zjb.wxlisener.until.WechatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.yaml.snakeyaml.events.Event;

import java.io.IOException;
import java.util.UUID;

@Service
public class LoginServiceimpl implements LoginService {
    @Autowired
    LoginMapper loginMapper;

    @Override
    public void addUser(User user) throws IOException {
        loginMapper.addUser(user);
    }

    @Override
    public String addimgUrl(MultipartFile imgUrl,String openid) throws Exception {
        String key = imgUrl.getOriginalFilename();
        String fileName = UUID.randomUUID().toString() + key.substring(key.lastIndexOf("."));
        System.out.println(fileName);
        boolean success = QiNiuUtil.uploadMultipartFile(imgUrl,fileName,false);
        if (success==true){
            String url = "http://se6frhgvz.hd-bkt.clouddn.com/"+fileName;
            loginMapper.addimgUrl(url,openid);
            System.out.println("1111111111111111111111");
            return url;
        }
        return null;
    }

    @Override
    public User checkUser(String code) {
        System.out.println(code);
        JsonObject jsonObject = WechatUtil.getOpenId(code);
        System.out.println(jsonObject);
        String openid = jsonObject.get("openid").getAsString();
        User user = loginMapper.checkUser(openid);
        return user;
    }

    @Override
    public PCuser PClogin(PCuser pCuser) {
        return loginMapper.PClogin(pCuser);
    }

}
